package com.holystone.splunk.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author huyan
 * @date 2017/9/14
 * @desc: 时间格式化工具类
 */
public class DateFormatUtils {

    public static final String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DEFAULT_FORMAT_DATE = "yyyy-MM-dd";

    /**
     * 将时间字符串转化为时间戳
     * @param dateStr
     * @return
     */
    public static Long dateStr2TimeStamp(String dateStr) throws ParseException {
        return str2Date(dateStr).getTime();
    }

    /**
     * 将时间字符串转化为时间戳
     * @param dateStr
     * @param format
     * @return
     */
    public static Long dateStr2TimeStamp(String dateStr, String format) throws ParseException {
        return str2Date(dateStr, format).getTime();
    }

    /**
     * 将时间戳转化为时间字符串
     * @param timeStamp
     * @param format
     * @return
     */
    public static String timeStamp2DateStr(Long timeStamp, String format){
        if (format == null || format == "")
            format = DEFAULT_FORMAT;
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(timeStamp));
    }

    /**
     * 将时间戳转化为时间字符串
     * @param timeStamp
     * @return
     */
    public static String timeStamp2DateStr(Long timeStamp) {
        return timeStamp2DateStr(timeStamp, DEFAULT_FORMAT);
    }

    /**
     * 将时间字符串转化为时间
     * @param dateStr
     * @param format
     * @return
     */
    public static Date str2Date(String dateStr, String format) throws ParseException {
        if (format == null || format == "")
            format = DEFAULT_FORMAT;
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.parse(dateStr);
    }

    /**
     * 将时间字符串转化为时间
     * @param dateStr
     * @return
     */
    public static Date str2Date(String dateStr) throws ParseException {
        return str2Date(dateStr, DEFAULT_FORMAT);
    }

    /**
     * 将时间转化为时间字符串
     * @param date
     * @param format
     * @return
     */
    public static String date2Str(Date date, String format){
        if(format==null||format.equals("")){
            format = DEFAULT_FORMAT;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    /**
     * 将时间转化为时间字符串
     * @param date
     * @return
     */
    public static String date2Str(Date date) {
        return date2Str(date, DEFAULT_FORMAT);
    }

    /**
     * 将unix时间戳转为java下的时间字符串
     * @param unixTime
     * @return
     */
    public static String unixTimeStamp2DateStr(String unixTime) {
        if (unixTime == null)
            return "";
        Long unix = Long.parseLong(unixTime);
        return timeStamp2DateStr(unix * 1000);
    }

    /**
     * 将java格式的日期函数转成C++中日期格式
     * @param date
     * @return
     * @throws ParseException
     */
    public static String javaDate2CDate(Date date) {
        return date2Str(date, "yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
    }
}
